//
//  TrainManage_HeaderSection_View.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/10/12.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "TrainManage_HeaderSection_View.h"
@interface TrainManage_HeaderSection_View()
@property (nonatomic,weak) UIView *backView;
@property (nonatomic,weak) UILabel *titleLabel;
@property (nonatomic,weak) UILabel *lineLabel;
@property (nonatomic,strong) UILabel *shadowLabel;
@end

@implementation TrainManage_HeaderSection_View

- (instancetype)initWithTitle:(NSString *)title{
    self = [super init];
    if (self) {
        [self createSubViewWithTitle:title];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubViewWithTitle:(NSString *)title{
    self.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor whiteColor];
    _backView = backView;
    [self addSubview:self.backView];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = [UIFont systemFontOfSize:LoginFont];
    titleLabel.textColor = [UIColor colorWithHexString:@"#282828"];
    titleLabel.text = title;
    _titleLabel = titleLabel;
    [self.backView addSubview:self.titleLabel];
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleBtn setBackgroundImage:[UIImage imageNamed:@"TrainManagementCancleAll"] forState:UIControlStateNormal];
    [cancleBtn setBackgroundImage:[UIImage imageNamed:@"TrainManagementRecoveryImage"] forState:UIControlStateSelected];
    _cancleBtn = cancleBtn;
    [self.backView addSubview:self.cancleBtn];
    
    UILabel *lineLabel = [[UILabel alloc] init];
    lineLabel.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
    _lineLabel = lineLabel;
    [self.backView addSubview:self.lineLabel];
    
    self.shadowLabel = [[UILabel alloc] init];
    self.shadowLabel.backgroundColor = RGBA(230, 230, 230, 1);
    self.shadowLabel.textColor = [UIColor whiteColor];
    self.shadowLabel.textAlignment = NSTextAlignmentCenter;
    self.shadowLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    self.shadowLabel.text = @"已过期";
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 162*SCALEUNIT, 64*SCALEUNIT) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10*SCALEUNIT, 10*SCALEUNIT)];
    CAShapeLayer *shaperLayer = [[CAShapeLayer alloc] init];
    shaperLayer.frame = CGRectMake(0, 0, 162*SCALEUNIT, 64*SCALEUNIT);
    shaperLayer.path = bezierPath.CGPath;
    self.shadowLabel.layer.mask = shaperLayer;
    
    //布局
    WS(ws);
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(ws);
        make.height.mas_equalTo(100*SCALEUNIT);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView).with.offset(35*SCALEUNIT);
        make.centerY.mas_equalTo(ws.backView);
        make.height.mas_equalTo(ws.backView).multipliedBy(0.5);
        make.width.mas_equalTo(195*SCALEUNIT);
    }];
    [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(ws.titleLabel);
        make.right.mas_equalTo(ws).with.offset(-35*SCALEUNIT);
        make.size.mas_equalTo(CGSizeMake(162*SCALEUNIT, 64*SCALEUNIT));
    }];
    [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(ws.backView);
        make.height.mas_equalTo(SCALEUNIT);
    }];
}

#pragma mark ==========获取数据==========
- (void)setDic:(NSDictionary *)dic{
    _dic = dic;
    if (_dic!=nil) {
        if ([self compareOneDay:[_dic objectForKey:@"selectDate"]]==2) {
            if (self.cancleBtn.hidden==NO) self.cancleBtn.hidden = YES;
            WS(ws);
            [self.backView addSubview:self.shadowLabel];
            [self.shadowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(ws.titleLabel);
                make.right.mas_equalTo(ws).with.offset(-35*SCALEUNIT);
                make.size.mas_equalTo(CGSizeMake(162*SCALEUNIT, 64*SCALEUNIT));
            }];
            return;
        } else {
            [self.shadowLabel removeFromSuperview];
            if (self.cancleBtn.hidden==YES) self.cancleBtn.hidden = NO;
        }
        BOOL solution = YES;
        NSArray *tempArr = [_dic objectForKey:@"schoolTimeList"];
        for (NSDictionary *dic in tempArr) {
            if (![[dic allKeys] containsObject:@"recordId"]) {
                solution = NO;
            }
        }
        
        if (solution) {
            self.cancleBtn.selected = YES;
        } else {
            self.cancleBtn.selected = NO;
        }
    }
}

#pragma mark ==========判断当前日期是否过期( 0相等,返回1没过期,2没过期 )==========
- (NSInteger)compareOneDay:(NSString *)oneDay{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *anotherDayStr = [dateFormatter stringFromDate:[NSDate date]];
    NSDate *dateA = [dateFormatter dateFromString:oneDay];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    if (result == NSOrderedDescending) {
        //在指定时间之后 没过期
        return 1;
    }
    else if (result == NSOrderedAscending){
        //在指定时间之前 已过期
        return 2;
    }
    //刚好时间一样.
    return 0;
}



@end
